package createTemplet;

import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.runtime.RuntimeConstants;
import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;

import java.io.StringWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * User: cool
 * Date: 2018/5/11
 * Time: 10:41
 */
public class Main {
    public static void main(String[] args){
        VelocityEngine ve = new VelocityEngine();
        ve.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
        ve.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());

        ve.init();
        // 获取模板文件
        Template t = ve.getTemplate("createTemplet/mapper.vm");
        // 设置变量
        VelocityContext ctx = new VelocityContext();
        ctx.put("namespace", "Velocity");
        ctx.put("tableName", "test_table".toUpperCase());
        List list = new ArrayList();
        Map map = new HashMap();
        map.put("name","id");//字段名
        map.put("column","id");//vo属性
        Map map1 = new HashMap();
        map1.put("name","name");
        map1.put("column","name");
        list.add(map);
        list.add(map1);

        ctx.put("fields", list);


        // 输出
        StringWriter sw = new StringWriter();
        t.merge(ctx,sw);

        System.out.println(sw.toString());

    }
}
